package com.example.college;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.college.Courses.LoadAllCourses;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class Student extends ListActivity {

	
	        // Progress Dialog
			private ProgressDialog pDialog;

			// Creating JSON Parser object
			JSONParser jParser = new JSONParser();

			ArrayList<HashMap<String, String>> reg_coursesList;
			
			// url to get registired Courses list
			private static String url_reg_courses = "http://192.168.140.1:8080/WebApplication2/webresources/doctor/";
			
			// JSON Node names
			private static final String TAG_SUCCESS = "success";
			private static final String TAG_REG_COURSES = "reg_courses";
			private static final String TAG_GRADE = "grade";
			private static final String TAG_NAME = "name";
			
			
			
			// courses JSONArray
			JSONArray courses = null;

			
			TextView studentName;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_student);
		
		
		studentName = (TextView) findViewById(R.id.student_name);
		studentName.setText(getIntent().getExtras().getString("id"));
		Button btnadddrop=(Button) findViewById(R.id.add_drop);
		btnadddrop.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View view) {
				Intent intent=new Intent(getApplicationContext(),Courses.class);
				startActivity(intent);
				
			}
		});
		
	         	// Hashmap for ListView
				reg_coursesList = new ArrayList<HashMap<String, String>>();
				
				// Loading courses in Background Thread
						new LoadRegCourses().execute();
	
	
	
	}

	/**
	 * Background Async Task to Load registired courses by making HTTP Request
	 * */
	
	class LoadRegCourses extends AsyncTask<String, String, String>{
		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pDialog = new ProgressDialog(Student.this);
			pDialog.setMessage("Loading Registired Courses. Please wait...");
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(false);
			pDialog.show();
		}

		/**
		 * getting Registired courses from url
		 * */
		
		@Override
		protected String doInBackground(String... arg0) {
			// Building Parameters
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			
			url_reg_courses = url_reg_courses+studentName.getText();

			// getting JSON string from URL
			JSONObject json = jParser.makeHttpRequest(url_reg_courses, "GET", params);
			
			// Check your log cat for JSON reponse
			Log.d("registired courses: ", json.toString());
			
			try {
				// Checking for SUCCESS TAG
				//int success = json.getInt(TAG_SUCCESS);

				if (json!= null) {
					// courses found
					// Getting Array of Courses
					//courses = json.getJSONArray(TAG_REG_COURSES);

					// looping through All Products
					
						// Storing each json item in variable
						
					
//						

						// creating new HashMap
						HashMap<String, String> map = new HashMap<String, String>();

						// adding each child node to HashMap key => value
						map.put(TAG_GRADE,json.getString("g1") );
						map.put(TAG_NAME, json.getString("c1"));

						// adding HashList to ArrayList
						reg_coursesList.add(map);
						
						map = new HashMap<String, String>();
						map.put(TAG_GRADE,json.getString("g2") );
						map.put(TAG_NAME, json.getString("c2"));

						// adding HashList to ArrayList
						reg_coursesList.add(map);
						
						map = new HashMap<String, String>();
						map.put(TAG_GRADE,json.getString("g3") );
						map.put(TAG_NAME, json.getString("c3"));

						// adding HashList to ArrayList
						reg_coursesList.add(map);
						
						

						// adding HashList to ArrayList
						reg_coursesList.add(map);
						
						map = new HashMap<String, String>();
						map.put(TAG_GRADE,json.getString("g4") );
						map.put(TAG_NAME, json.getString("c4"));

						// adding HashList to ArrayList
						reg_coursesList.add(map);
						
						
						map = new HashMap<String, String>();
						map.put(TAG_GRADE,json.getString("g5") );
						map.put(TAG_NAME, json.getString("c5"));

						// adding HashList to ArrayList
						reg_coursesList.add(map);
						
						map = new HashMap<String, String>();
						map.put(TAG_GRADE,json.getString("g6") );
						map.put(TAG_NAME, json.getString("c6"));

						// adding HashList to ArrayList
						reg_coursesList.add(map);
						
					
					
				} else {
					// no registired courses found
					Toast toast= Toast.makeText(getApplicationContext(), "No Regstired Courses Found", Toast.LENGTH_LONG);
					toast.show();

					
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
			
			
			return null;}
		/**
		 * After completing background task Dismiss the progress dialog
		 * **/
		protected void onPostExecute(String file_url) {
			// dismiss the dialog after getting all products
			pDialog.dismiss();
			// updating UI from Background Thread
			runOnUiThread(new Runnable() {
				public void run() {
					/**
					 * Updating parsed JSON data into ListView
					 * */
					ListAdapter adapter = new SimpleAdapter(
							Student.this, reg_coursesList,
							R.layout.course_list, new String[] { TAG_NAME,
									TAG_GRADE},
							new int[] { R.id.course_name, R.id.course_grade });
										
					
					// updating list view
					setListAdapter(adapter);
				}
			});
		}
		
		
		

		}
}
